/*
 * TraceabilityTreeEntityResolver.java
 *
 * $Id: TraceabilityTreeEntityResolver.java,v 1.1 2009-03-02 15:31:23 mario Exp $
 */
package org.ceteca.explica.core.traceability.tree.parser;

import java.io.IOException;
import java.net.URL;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import org.ceteca.explica.core.traceability.InterfaceTraceabilityTree;
import org.ceteca.explica.core.util.LoggerFactory;

/**
* This class implements a SAX Entity Resolver to update the DTD used in the Traceability Tree
* XML Parsing to the local version. That way the application can work even with
* no Internet connection and the parsing is faster.
* <br/>
* Responsibilities:
* <ul>
*  <li>Translate the web URL of the DTD to a local URI.</li>
* </ul>
* <br/>
* @author Mario García García <mario@imagos.es>
 * Copyright (c) 2007 Fundación Centro Tecnolóxico da Carne
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <br/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <br/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
public class TraceabilityTreeEntityResolver implements EntityResolver {
	/**
	 * Allow the application to resolve external entities. Its the implementation
	 * of the resolveEntity method of the EntityResolver interface. In this method
	 * the DTD external entity will be redirected to its local version.
	 * @param publicId String, The public identifier of the external entity being referenced, 
	 * or null if none was supplied.
	 * @param systemId String, The system identifier of the external entity being referenced.
	 * @return InputSource An InputSource object describing the new input source, or null to request that the 
	 * parser open a regular URI connection to the system identifier.
	 */
	public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
		if (systemId != null && systemId.equals("http://traceabilityTree.dtd")) {
			URL url = this.getClass().getResource("/org/ceteca/explica/core/traceability/tree/parser/traceabilityTree.dtd"); 
			LoggerFactory.getLogger(InterfaceTraceabilityTree.TREEVIEW_LOG_CATEGORY).
    			info(String.format(InterfaceTraceabilityTree.TREEVIEW_LOG_RESOLVE_DTD, (url==null?"null":url.getPath())));
			return new InputSource(this.getClass().getResourceAsStream("/org/ceteca/explica/core/traceability/tree/parser/traceabilityTree.dtd"));
		}
		else {
			return null;
		}
	}

	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: TraceabilityTreeEntityResolver.java,v 1.1 2009-03-02 15:31:23 mario Exp $";
	}
}
